/** The Base sprite class
 * which is the basis for
 * all 2d pictures on the
 * screen. All sprites are
 * placed within a box
 * even if it is a complex
 * shape, it still has a box
 * with transparency applied.
 * 
 * Various functions are given
 * to find the width and height
 * as well as draw.
 * 
 * This is where the base of 
 * OpenGL drawing is hidden.
 * 
 * 
 * @author Andrew
 * @creator Andrew
 * LAST UPDATED - Andrew 2/4/12
 */


// For the Error handling
import java.io.IOException;

// OpenGL Bindings for drawing
import org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL11;

// For loading textures
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;


public class Sprite {

	// 2d Texture for the sprite
	private Texture	texture;

	// Width in pixels (should be int but double makes me happy)
	private double			width;

	// Height in pixels
	private double			height;
	

        // create the sprite, takes a path to load
        // returns error if it cant find the file and closes
	public Sprite(String path) {
    try {
      texture = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("data/" + path));
      width = texture.getImageWidth();
      height = texture.getImageHeight();
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
	}

        // Return the width
	public double getWidth() {
		return texture.getImageWidth();
	}

        // Return the Height
	public double getHeight() {
		return texture.getImageHeight();
	}

        // Draw the Sprite at x,y
	public void draw(double xd, double yd) {
            // Convert to floats for OpenGL
            float x = (float) xd;
            float y = (float) yd;
             
		// store the current model matrix
		GL11.glPushMatrix();

		// bind the texture
		texture.bind();

		// move to x,y (no third dimension)
		GL11.glTranslatef(x, y, 0);

		// draw a quad to the same size as the sprite
		// with texture coords - to display correctly
		GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(0, texture.getHeight());
			GL11.glVertex2f(0, 0);

			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(0, (float) height);

			GL11.glTexCoord2f(texture.getWidth(), 0);
			GL11.glVertex2f((float) width, (float) height);

			GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
			GL11.glVertex2f((float) width, 0);
		}
		GL11.glEnd();

		// restore the model matrix
		GL11.glPopMatrix();
	}
	
	// draw at an angle
	public void draw(double xd, double yd, double angle) {
        // Convert to floats for OpenGL
        float x = (float) xd;
        float y = (float) yd;
         
	// store the current model matrix
	GL11.glPushMatrix();

	// bind the texture
	texture.bind();

	// move to x,y (no third dimension)
	GL11.glTranslatef(x, y, 0);
	
	// Rotate via the angle
	GL11.glRotatef((float)angle, 0, 0, 1);

	// draw a quad to the same size as the sprite
	GL11.glBegin(GL11.GL_QUADS);
	{
		GL11.glTexCoord2f(0, texture.getHeight());
		GL11.glVertex2f(0, 0);

		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2f(0, (float) height);

		GL11.glTexCoord2f(texture.getWidth(), 0);
		GL11.glVertex2f((float) width, (float) height);

		GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
		GL11.glVertex2f((float) width, 0);
	}
	GL11.glEnd();

	// restore the model matrix
	GL11.glPopMatrix();
}
	
	// reverse the picture
	public void drawReversed(double xd, double yd) {
        // Convert to floats for OpenGL
        float x = (float) xd;
        float y = (float) yd;
         
	// store the current model matrix
	GL11.glPushMatrix();

	// bind the texture
	texture.bind();

	// move to x,y (no third dimension)
	GL11.glTranslatef(x, y, 0);

	// draw a quad to the same size as the sprite
	// but reverse the texture coords for reversed
	GL11.glBegin(GL11.GL_QUADS);
	{
		
		GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
		GL11.glVertex2f(0, 0);

		GL11.glTexCoord2f(texture.getWidth(), 0);
		GL11.glVertex2f(0, (float) height);

		GL11.glTexCoord2f(0,0);
		GL11.glVertex2f((float) width, (float) height);

		
		GL11.glTexCoord2f(0, texture.getHeight());
		GL11.glVertex2f((float) width, 0);
	}
	GL11.glEnd();

	// restore the model matrix
	GL11.glPopMatrix();
}
	
	// and draw reversed at an angle.
	public void drawReversed(double xd, double yd, double angle) {
        // Convert to floats for OpenGL
        float x = (float) xd;
        float y = (float) yd;
         
	// store the current model matrix
	GL11.glPushMatrix();

	// bind the texture
	texture.bind();

	// move to x,y (no third dimension)
	GL11.glTranslatef(x, y, 0);
	
	// Rotate via the angle
		GL11.glRotatef((float)angle, 0, 0, 1);

	// draw a quad to the same size as the sprite
	GL11.glBegin(GL11.GL_QUADS);
	{
		
		GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
		GL11.glVertex2f(0, 0);

		GL11.glTexCoord2f(texture.getWidth(), 0);
		GL11.glVertex2f(0, (float) height);

		GL11.glTexCoord2f(0,0);
		GL11.glVertex2f((float) width, (float) height);

		
		GL11.glTexCoord2f(0, texture.getHeight());
		GL11.glVertex2f((float) width, 0);
	}
	GL11.glEnd();

	// restore the model matrix
	GL11.glPopMatrix();
}
}
